import React from "react";
import { createStore, applyMiddleware } from "redux";
import { Provider } from "react-redux";
import { Router, Route, browserHistory, hashHistory } from "react-router";
import { syncHistoryWithStore } from "react-router-redux";
import promiseMiddleware from 'redux-promise';

import { INITIAL_STATE, ROUTER_INITIAL_STATE } from './common';

import AllRoutes from "./router/index.jsx";
import allReducers from "./reducer";

const store = createStore(allReducers, INITIAL_STATE, applyMiddleware(promiseMiddleware));

// Create an enhanced history that syncs navigation events with the store
const history = syncHistoryWithStore(browserHistory, store, {
    selectLocationState(state = ROUTER_INITIAL_STATE) {
        let routing = state.get('routing');
        if (routing) {
            return routing.toJS();
        }
        return routing;
    }
});

const App = () => ({
    render() {
        return (<Provider store={ store }>
                    <AllRoutes history={ history } />
                </Provider>);
    }
});

export default App;